//
//  LGScrollTextView.m
//  UniverseProject
//
//  Created by king on 2021/6/18.
//  Copyright © 2021 Cloud2209. All rights reserved.
//

#import "LGScrollTextView.h"

@interface LGScrollTextView ()<CAAnimationDelegate>

@property (nonatomic, strong) UILabel * contentLabel;

@end


@implementation LGScrollTextView

- (void)dealloc {
    NSLog(@"跑马灯消失");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_contentLabel];
    
    //超出部分不显示
    self.layer.masksToBounds = YES;
    //设置默认值
    self.speed = 0.5;
    //解决进入后台然后回来就停了
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)startAnimation {
    NSLog(@"动画调用");
    //根据内容得到_contentLabel的frame
    [_contentLabel sizeToFit];
    //居中显示
    CGRect frame = _contentLabel.frame;
    frame.origin.y = (self.bounds.size.height - frame.size.height)/2;
    _contentLabel.frame = frame;
    
    [self setNeedsLayout];
}

- (void)setText:(NSString *)text {
    _text = text;
    _contentLabel.text = text;
    //根据内容得到_contentLabel的frame
    [_contentLabel sizeToFit];
    //居中显示
    CGRect frame = _contentLabel.frame;
    frame.origin.y = (self.bounds.size.height - frame.size.height)/2;
    _contentLabel.frame = frame;

}

- (void)setFont:(UIFont *)font {
    _font = font;
    _contentLabel.font = font;
}

- (void)setSpeed:(CGFloat)speed {
    _speed = speed;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat sizeWidth = CGRectGetWidth(_contentLabel.frame);
    
    [self.contentLabel.layer removeAnimationForKey:@"keyFrame"];
    //如果文字能够显示完全 则不用滚动显示
    if (sizeWidth<= self.bounds.size.width) {
        return;
    }
    
    //添加帧动画 实现滚动效果 其实就是改变x的值
    CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
    keyFrame.delegate = self;
    keyFrame.keyPath = @"transform.translation.x";
    keyFrame.values = @[@(0), @(-sizeWidth + self.bounds.size.width)];
    keyFrame.repeatCount = 0;
    keyFrame.autoreverses = NO;
//    keyFrame.removedOnCompletion = NO;
    keyFrame.duration =  5.f;
//    keyFrame.duration =  12+(1/self.speed) * self.contentLabel.text.length*0.1;
    NSLog(@"layoutSubviews _speed：%f duration:%f lenth:%lu",_speed,keyFrame.duration,self.contentLabel.text.length);
    //滚动速度改变 可以实现滚动速度由慢变快或者由快变慢
//    keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithControlPoints:0 :0 :0.5 :0.5]];
    [self.contentLabel.layer addAnimation:keyFrame forKey:@"keyFrame"];

}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSLog(@"动画停止了");
    self.contentLabel.frame = self.bounds;
    
}

@end
